package com.example.demo.myValidator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author ml
 * @create 2017-07-10--15:22
 */
public class FlagValidatorClass implements ConstraintValidator<FlagValidator,Object> {

    //临时变量保存Flag值列表
    private String values;

    @Override
    public void initialize(FlagValidator flagValidator) {
        //将注解内配置的值赋给临时变量
        this.values=flagValidator.values();

    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        //分割定义的有效值
        String[] value_array=values.split(",");
        boolean Flag=false;
        //遍历对比有效值
        for(int i=0;i<value_array.length;i++){
            if(value_array[i].equals(o)){
                Flag=true;
                break;
            }
        }
        return Flag;
    }
}
